import os
from PIL import Image
# 用于将原始图像进行压缩存储
def resize_images(source_dir, target_dir, size):
    if not os.path.exists(target_dir):
        os.makedirs(target_dir)
    subfolders = os.listdir(source_dir)

    for subfolder in subfolders:
        source_subfolder = os.path.join(source_dir, subfolder)
        target_subfolder = os.path.join(target_dir, subfolder)
        
        if not os.path.exists(target_subfolder):
            os.makedirs(target_subfolder)

        for filename in os.listdir(source_subfolder):
            if filename.endswith('.jpg') or filename.endswith('.png') or filename.endswith('.JPG'):
                img_path = os.path.join(source_subfolder, filename)
                img = Image.open(img_path)
                img = img.resize(size, Image.ANTIALIAS)
                img.save(os.path.join(target_subfolder, filename))

resize_images('data', 'newdata', (224, 224))
